//
//  YJGifEmotionListView.m
//  YJB
//
//  Created by YJB on 2017/1/21.
//  Copyright © 2017年 YJB. All rights reserved.
//

#import "YJGifEmotionListView.h"
#import "YJGifEmotionBtn.h"
#import "YJGifEmotionPageView.h"

@interface YJGifEmotionListView()<UIScrollViewDelegate>
@property(nonatomic,weak)UIPageControl *page;
@property(nonatomic,weak)UIScrollView *scrollView;

@end
@implementation YJGifEmotionListView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        //scrollView
        UIScrollView *scrollView = [[UIScrollView alloc]init];
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.bounces = NO;
        scrollView.pagingEnabled = YES;
        scrollView.delegate = self;
        [self addSubview:scrollView];
        self.scrollView = scrollView;
        
        //分页控件
        UIPageControl *pageControl = [[UIPageControl alloc]init];
        pageControl.hidesForSinglePage = YES;
        pageControl.pageIndicatorTintColor = kUIColorFromRGB(0xdedede);
        [pageControl addTarget:self action:@selector(pageControlClick:event:) forControlEvents:UIControlEventTouchUpInside];
        pageControl.currentPageIndicatorTintColor = kUIColorFromRGB(0xff783c);
        pageControl.backgroundColor = [UIColor clearColor];
        [self addSubview:pageControl];
        self.page = pageControl;
        
    }
    return self;
}

- (void)pageControlClick:(UIPageControl *)page event:(UIEvent *)touchs
{
    UITouch *touch = [[touchs allTouches] anyObject];
    CGPoint touchPoint = [touch locationInView:page];
    //计算第一个点距离page左边的距离(15是一个点所占据的空间大小,点的默认大小是7，点与点之间的间距是8）
    CGFloat left = (page.width - 15.0 * page.numberOfPages) / 2;
    page.currentPage = (int) (touchPoint.x - left) / 15;

    self.scrollView.contentOffset = CGPointMake(self.scrollView.width * page.currentPage, 0);
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    //page
    self.page.width = self.page.numberOfPages * 15 + 20;
    self.page.height = 37;
    self.page.centerX = self.centerX;
    self.page.y = self.height - self.page.height;
    
    //scrollView
    self.scrollView.x = self.scrollView.y = 0;
    self.scrollView.width = self.width;
    self.scrollView.height = self.page.y;
    
    //全部的emotionPageView
    NSInteger count = self.scrollView.subviews.count;
    for (int k = 0; k < count ; k ++)
    {
        YJGifEmotionPageView *emotionView = self.scrollView.subviews[k];
        emotionView.width = self.scrollView.width;
        emotionView.height = self.scrollView.height;
        emotionView.x = k * self.scrollView.width;
        emotionView.y = 0;
    }
}

-(void)setEmotions:(NSArray *)emotions
{
    _emotions = emotions;
    
    //回到最初的表情页
    self.page.currentPage = 0;
    self.scrollView.contentOffset = CGPointMake(0, 0);
    //移除scrollView上面的所有子视图
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    //计算分页数
    if (emotions.count % EachPageNUmber == 0)
    {
        self.page.numberOfPages = emotions.count/EachPageNUmber;
    }else
        self.page.numberOfPages = emotions.count/EachPageNUmber + 1;
    
    //emotionPageView
    for (int k = 0; k < self.page.numberOfPages; k ++)
    {
        YJGifEmotionPageView *emotionView = [[YJGifEmotionPageView alloc]init];
        
        NSRange range;
        range.location = k * EachPageNUmber;
        //计算剩下的表情，并且看看够不够20个
        NSUInteger remaining = emotions.count - range.location;
        if (remaining < EachPageNUmber)
            range.length = remaining;
        else
            range.length = EachPageNUmber;
        //将每一页需要展示的表情赋值给pageView
        emotionView.emotions = [self.emotions subarrayWithRange:range];
        [self.scrollView addSubview:emotionView];
    }
    //设置scrollView的滚动范围
    self.scrollView.contentSize = CGSizeMake(self.page.numberOfPages * self.width, 0);
    
    //重新布局子视图的大小
    [self setNeedsLayout];
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.page.currentPage = (int)(scrollView.contentOffset.x / IPHONE_W + 0.5);
}

@end
